<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
    >
      <el-form-item
        label="代理商名称"
        prop="agentName"
      >
        <el-input
          v-model="queryParams.agentName"
          placeholder="请输入代理商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="状态"
        prop="currentStatus"
      >
        <el-select
          v-model="queryParams.currentStatus"
          placeholder="全部"
          clearable
        >
          <el-option
            v-for="dict in selectType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>
    <el-button
      type="primary"
      style="float:right; margin-bottom: 20px;"
      @click="addDlshang"
    >新增代理商</el-button>
    <el-table
      v-loading="loading"
      :data="initTable"
      :height="tableHeight"
      style="width: 100%"
    >
      <el-table-column
        label="代理商名称"
        align="center"
        prop="agentName"
        width="180"
        :show-overflow-tooltip="true"
      ></el-table-column>

      <el-table-column
        label="注册来源"
        align="center"
        prop="factoryName"
        :show-overflow-tooltip="true"
      ></el-table-column>

      <el-table-column
        label="联系电话"
        align="center"
        prop="agentPhone"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="负责人"
        align="center"
        prop="responsiblePerson"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="负责人电话"
        align="center"
        prop="responsibleTel"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="账户名"
        align="center"
        prop="accountName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="当前状态"
        align="center"
        prop="currentStatus"
        :show-overflow-tooltip="true"
      >
        <template #default="{row}">
          {{ handleStatusChange(row) }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleManualExecution(scope.row)"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <AgentCreate ref="agentCreateRef" @clickChange="handleCircle"></AgentCreate>
  </div>
</template>
  
<script>
import { list} from '@/api/agent/index'
import AgentCreate from './modules/AgentCreate'
export default {
  name: "Agent",
  components : { AgentCreate },
  data() {
    return {
      //初始化表格
      initTable: [],
      tableHeight: window.innerHeight - 300,
      loading: false,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        agentName: undefined,
        currentStatus: undefined,
      },
      selectType: [
        {
          label: '全部',
          value: ''
        },
        {
          label: '启用',
          value: '0'
        },
        {
          label: '停用',
          value: '1'
        }
      ],
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 新增
    addDlshang() {
      this.$refs.agentCreateRef.sync(null, false)
    },
    //查询列表
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    getList() {
      this.loading = true
      list(this.queryParams).then(response => {
        this.initTable = response.rows;
        this.total = response.total;
      }).finally(() => {
        this.loading = false
      })
    },
    handleStatusChange(row) {
      let name = ''
      this.selectType.map(item => {
        if (row.currentStatus === item.value) {
          name = item.label
        }
      })
      return name
    },
    //重置列表
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        agentName: undefined,
        currentStatus: undefined,
      }
      this.getList()
    },
    //编辑
    handleUpdate(row) {
      this.$refs.agentCreateRef.sync(row, false)
    },
    //查看
    handleManualExecution(row) {
      this.$refs.agentCreateRef.sync(row, true)
    },
    handleCircle() {
      this.queryParams.pageNum = 1
      this.getList()
    }
  }
}
</script>
  
<style scoped lang="scss">
::v-deep .el-table .el-form-item__content {
  margin-left: 0px !important;
}

::v-deep .el-table .el-form-item {
  margin-bottom: 0px;
}
</style>
  